package logic

import (
	"context"
	"sus/models/mysql"
	"sus/pkg"

	"sus/internal/svc"
	"sus/sus"

	"github.com/zeromicro/go-zero/core/logx"
)

type AuthLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthLogic {
	return &AuthLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AuthLogic) Auth(in *sus.AuthRequest) (*sus.AuthResponse, error) {
	// todo: add your logic here and delete this line
	var r mysql.RealName
	var err error
	r.UserId = int(in.UserId)
	if err = r.GetRealNameId(); err != nil {
		logx.Error("用户不存在")
		return nil, err
	}
	if r.ID > 0 {
		logx.Error("书名认证失败")
		return nil, err
	}
	r.RealName = in.RealName
	r.IDCard = in.IDCard
	if pkg.Auth(in.IDCard, in.RealName) {
		logx.Error("失败")
		return nil, err
	}
	if err = r.CreateRealName(); err != nil {
		logx.Error("实名认证失败")
		return nil, err
	}
	return &sus.AuthResponse{
		Id: int64(r.ID),
	}, nil
}
